<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-02-28
 * Time: 14:31
 */

class UserModel extends MY_Model{
    public $table = 't_user';
    public function __construct(){
        parent::__construct();
    }


    public function user_login($username,$password){
        $where = array("mobile" => $username,'password'=>$password);
        $arr = $this->query_one($where);
        $result = array(
            'status' => -1,// 0：正常 -1：用户名/密码错误 -2：没有权限
            'data'   => array()
        );
        if(empty($arr)){
            return $result;
        }
        $where = array("uid" => $arr['uid']);
        $admin = $this->UserAdmin->query_one($where);
        if(empty($admin)){
            $result['status'] = -2;
        }else{
            $result = array(
                'status' => 0,
                'data'   => $arr
            );
        }
        return $result;
    }

    /**
     * 获取用户详情
     * @param $uid
     * @return array
     * @throws Exception
     */
    public function getIdUser($uid){
        if(empty($uid)){
            return array();
        }
        $where = array("uid" => $uid);
        return $this->query_one($where);
    }

    /**
     * 查询用户的详细信息带统计
     * name: getUserInfo
     * @param $uid
     * @return array
     * @throws Exception
     * @Author:taiyang
     */
    public function getUserInfo($uid){
        if(empty($uid)){
            return array();
        }
        $sql="select u.*,c.*,i.path from ".$this->table." u left join t_user_count c on u.uid=c.uid left join t_img i on i.type=2 and i.id=u.avator_id where 1 ";
        $sql.=" and u.uid=".$uid;
        return $this->query_sql_one($sql);
    }

    public function getWhereSql($where){
        if(!empty($where)){
            foreach($where as $k => $v){
                if($k == "user_status"){
                    if($v == 6){
                        $this->db->where(" uid in (select uid from t_user_punish where type=6 )");
                    }else{
                        $this->db->where(" uid in (select uid from t_user_punish where type=".$v." and time_range in (1,2,3,4,5,7) )");
                    }
                }else if($k == "post_review"){
                    $this->db->where(" uid in (select uid from t_user_count where is_post_review=1 )");
                }else{
                    if(is_array($v)){
                        $this->db->where_in($k, $v);
                    }else{
                        $this->db->where($k, $v);
                    }
                }
            }
        }
    }
    /**
     * 分页列表
     * @param array $where
     * @param int $page
     * @param int $pagesize
     * @param array $orderBy
     * @return mixed
     */
    public function getList($where = array(), $page = 1, $pagesize = 20, $orderBy = array()){
        $this->getWhereSql($where);
        if(!empty($orderBy)){
            foreach($orderBy as $key => $value){
                $this->db->order_by($key, $value);
            }
        }
        if($page == 0){
            return $this->db->get($this->table)->result_array();
        }else{
            $index = $page - 1 < 0 ? 0 : ($page - 1) * $pagesize;
            return $this->db->limit($pagesize, $index)->get($this->table)->result_array();
        }
    }

    /**
     * 分页列表数量
     * @param array $where
     * @return mixed
     */
    public function getListNum($where = array()){
        $this->getWhereSql($where);
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    /**
     * 账号禁言封号禁闭
     * name: user_jin
     * @param string $type  4禁言5禁闭6封号
     * @param string $time_range
     * @param string $uid
     * @param array $loginUser
     * @return bool
     * @Author:taiyang
     */
    public function user_jin($type='',$time_range='',$uid='',$loginUser=array()){
        $typeArr = $this->UserPunishModel->type;
        $statusArr = $this->UserPunishModel->status;
        if($type == 6 && $time_range == 6){
            $this->UserPunishModel->del_data(array("uid" => $uid));
        }else{
            $userPunish = $this->UserPunishModel->query_one(array("uid" => $uid));
            if(!empty($userPunish)){
                $param = array("type" => $type, "time_range" => $time_range, "add_time" => time(), "update_uid" => $loginUser["uid"], "update_time" => time());
                $this->UserPunishModel->update_data($param, array("uid" => $uid));
            }else{
                $param = array("uid" => $uid, "type" => $type, "time_range" => $time_range, "add_time" => time(), "update_uid" => $loginUser["uid"], "update_time" => time());
                $this->UserPunishModel->insert_one($param);
            }
        }
        $param = array("uid" => $uid, "type" => $type, "time_range" => $time_range, "add_time" => time(), "update_uid" => $loginUser["uid"]);
        $this->UserPunishLogModel->insert_one($param);

        $desc = $typeArr[$type].":".$statusArr[$time_range];
        $this->AdminOpenationModel->insertLog($uid, 36, $desc);
        return true;
    }

    /**
     * 获取用户来源列表
     * @return array
     * @throws Exception
     */
    public function distinctSource(){
        $sql = "select distinct user_source from t_user where user_source !=''";
        return $this->query_sql_list($sql);
    }
}